package pres.moling.common.tool;

import java.util.Arrays;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串工具类
 *
 * @author moling
 */
public interface StrHelper {

    Pattern HUMP_PATTERN = Pattern.compile("[A-Z]");
    Pattern LINE_PATTERN = Pattern.compile("_(\\w)");

    /**
     * 下划线转驼峰
     */
    static String lineToHump(String str) {
        str = str.toLowerCase();
        Matcher matcher = LINE_PATTERN.matcher(str);
        StringBuilder sb = new StringBuilder();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /**
     * 驼峰转下划线
     */
    static String humpToLine(String str) {
        Matcher matcher = HUMP_PATTERN.matcher(str);
        StringBuilder sb = new StringBuilder();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /**
     * 若开头匹配则删除该匹配字符串
     */
    static String replaceStartWith(String str, String sc) {
        return str.startsWith(sc) ? str.replaceFirst(sc, "").trim() : str;
    }

    /**
     * 若结尾匹配则删除该匹配字符串
     */
    static String replaceEndWith(String str, String sc) {
        return str.endsWith(sc) ? str.substring(0, str.indexOf(sc)).trim() : str;
    }

    static boolean isEmpty(Object str) {
        return Objects.isNull(str) || isEmpty(String.valueOf(str));
    }

    /**
     * 字符串判空
     */
    static boolean isEmpty(CharSequence str) {
        return Objects.isNull(str) || str.length() == 0;
    }

    static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    static boolean isNotBlank(String str) {
        return !isBlank(str);
    }

    static boolean isBlank(String str) {
        return isEmpty(str.trim()) || "".contentEquals(str);
    }

    /**
     * 将字符串按照指定字符补足指定长度
     *
     * @param str       待操作字符串
     * @param minLength 字符串长度
     * @param filled    补充的字符
     * @param isPrefix  是否为前缀补足
     */
    static String complement(String str, int minLength, CharSequence filled, boolean isPrefix) {
        if (minLength <= 0) {
            throw new RuntimeException("要填充的字符串长度应该大于0");
        }
        if (isEmpty(filled)) {
            throw new RuntimeException("输入的填充字符不能为空");
        }
        str = isEmpty(str) ? "" : str;
        StringBuilder builder = new StringBuilder();
        int length = minLength - str.length();
        while (length >= builder.length()) {
            builder.append(filled);
        }
        builder = new StringBuilder(builder.substring(0, length));
        builder = isPrefix ? builder.insert(0, str) : builder.append(str);
        return builder.toString();

    }

    /**
     * 查看两个字符串是否有共同项
     *
     * @param src 源字符串
     * @param tar 目标字符串
     * @param sp  分割字符
     */
    static boolean commonItems(String src, String tar, String sp) {
        if (isBlank(src) || isBlank(tar) || isBlank(sp)) {
            return false;
        }
        String[] split = src.split(sp);
        for (String s : tar.split(sp)) {
            if (Arrays.asList(split).contains(s)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断 字符串 str 是否是以 sc 中的任一字符串开头
     *
     * @param sc  字符串列表
     * @param str 要校验的字符串
     * @return 是否
     */
    static boolean startWith(final String str, String... sc) {
        assert Objects.nonNull(sc);
        return Arrays.stream(sc).anyMatch(str::startsWith);
    }

    /**
     * 字符串包含列表中任一子串
     */
    static boolean containsIn(final String str, String... sc) {
        assert Objects.nonNull(sc);
        return Arrays.stream(sc).anyMatch(str::contains);
    }

    /**
     * 判断是否以指定字符列表之一结尾
     */
    static boolean endIn(final String str, String... sc) {
        assert Objects.nonNull(sc);
        return Arrays.stream(sc).anyMatch(str::endsWith);
    }
}
